package com.itheima.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.exception.ErrorResult;
import com.itheima.mall.pojo.Admin;
import com.itheima.mall.pojo.Menu;
import com.itheima.mall.pojo.RoleMenuRelation;
import com.itheima.mall.vo.PageResult;
import com.itheima.ums.mapper.MenuMapper;
import com.itheima.ums.mapper.RoleWithMenuMapper;
import com.itheima.ums.service.UmsMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UmsMenuServiceImpl implements UmsMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleWithMenuMapper roleWithMenuMapper;


    //添加后台菜单
    public Integer createMenu(Menu menu) {
        menu.setCreateTime(new Date());
        updateLevel(menu);
        return menuMapper.insert(menu);
    }

    private void updateLevel(Menu menu) {
        if (menu.getParentId() == 0) {
            //没有父菜单时为一级菜单
            menu.setLevel(0l);
        } else {
            //有父菜单时选择根据父菜单level设置
            Menu parentMenu = menuMapper.selectById(menu.getParentId());
            if (parentMenu != null) {
                menu.setLevel(parentMenu.getLevel() + 1);
            } else {
                menu.setLevel(0l);
            }
        }
    }

    //根据ID删除后台菜单
    public Integer deleteMenuById(Long id) {
        LambdaQueryWrapper<RoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenuRelation::getMenuId, id);
        boolean exists = roleWithMenuMapper.exists(queryWrapper);
        if (exists) {
            throw new BusinessException(ErrorResult.menuWithRoleError());
        }
        LambdaQueryWrapper<Menu> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Menu::getParentId, id);
        boolean exists1 = menuMapper.exists(queryWrapper1);
        if (exists1) {
            throw new BusinessException(ErrorResult.menuError());
        }
        return menuMapper.deleteById(id);

    }

    //分页查询后台菜单
    public PageResult getMenuPage(Long parentId, Integer pageNum, Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getParentId, parentId);
        queryWrapper.orderByDesc(Menu::getSort);

        menuMapper.selectPage(page, queryWrapper);

        return new PageResult(pageNum, pageSize, (int) page.getTotal(), page.getRecords());
    }

    //修改后台菜单
    public Integer updateMenuById(Long id, Menu menu) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getId, id);
        updateLevel(menu);
        return menuMapper.update(menu, queryWrapper);

    }

    //树形结构返回所有菜单列表
    public List<Menu> treeList() {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getParentId, 0);
        List<Menu> one = menuMapper.selectList(queryWrapper);
        one = one.stream().map(item -> {
            Menu vo = new Menu();
            BeanUtils.copyProperties(item, vo);
            LambdaQueryWrapper<Menu> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Menu::getParentId, item.getId());
            List<Menu> list = menuMapper.selectList(queryWrapper1);
            vo.setChildren(list);
            return vo;
        }).collect(Collectors.toList());


        return one;
    }

    //根据id获取菜单详情
    public Menu getMenuById(Long id) {

        return menuMapper.selectById(id);
    }

    //修改菜单显示状态
    public Integer updateHidden(Long id, Long hidden) {
        LambdaUpdateWrapper<Menu> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Menu::getHidden, hidden).eq(Menu::getId, id);
        return menuMapper.update(null, updateWrapper);
    }
}
